/*
 * param.h
 *
 *  Created on: 15/ago/2011
 *      Author: vito
 */

#ifndef PARAM_H_
#define PARAM_H_

#include <stdint.h>
#include <stdbool.h>
#include <pthread.h>

#include <defines.h>
#include <config.h>

#include <groundcontrol.h>

#include <mavlink.h>

/*
enum {
	PARAM_SYSTEM_ID = 0,
	PARAM_COMPONENT_ID,
	PARAM_SYSTEM_TYPE,
	PARAM_SW_VERSION,
	PARAM_IMU_RESET,

	PARAM_UART0_BAUD,
	PARAM_UART1_BAUD,
	PARAM_UART2_BAUD,
	PARAM_UART3_BAUD,
	PARAM_UART4_BAUD,

	PARAM_MIX_REMOTE_WEIGHT,
	PARAM_MIX_POSITION_WEIGHT,
	PARAM_MIX_POSITION_Z_WEIGHT,
	PARAM_MIX_POSITION_YAW_WEIGHT,
	PARAM_MIX_OFFSET_WEIGHT,
	PARAM_TRIMCHAN,

	PARAM_PID_ATT_P,
	PARAM_PID_ATT_I,
	PARAM_PID_ATT_D,
	PARAM_PID_ATT_LIM,
	PARAM_PID_ATT_AWU,

	PARAM_PID_POS_P,
	PARAM_PID_POS_I,
	PARAM_PID_POS_D,
	PARAM_PID_POS_LIM,
	PARAM_PID_POS_AWU,

	PARAM_PID_POS_Z_P,
	PARAM_PID_POS_Z_I,
	PARAM_PID_POS_Z_D,
	PARAM_PID_POS_Z_LIM,
	PARAM_PID_POS_Z_AWU,

	PARAM_PID_YAWPOS_P,
	PARAM_PID_YAWPOS_I,
	PARAM_PID_YAWPOS_D,
	PARAM_PID_YAWPOS_LIM,
	PARAM_PID_YAWPOS_AWU,

	PARAM_PID_YAWSPEED_P,
	PARAM_PID_YAWSPEED_I,
	PARAM_PID_YAWSPEED_D,
	PARAM_PID_YAWSPEED_LIM,
	PARAM_PID_YAWSPEED_AWU,

	PARAM_POSITIONSETPOINT_ACCEPT,
	PARAM_POSITION_TIMEOUT,
	PARAM_POSITION_SETPOINT_X,
	PARAM_POSITION_SETPOINT_Y,
	PARAM_POSITION_SETPOINT_Z,
	PARAM_POSITION_SETPOINT_YAW,
	PARAM_POSITION_YAW_TRACKING,

	PARAM_ATT_OFFSET_X,
	PARAM_ATT_OFFSET_Y,
	PARAM_ATT_OFFSET_Z,

	PARAM_VEL_OFFSET_X,
	PARAM_VEL_OFFSET_Y,
	PARAM_VEL_OFFSET_Z,

	PARAM_VEL_DAMP,
	PARAM_ATT_KAL_KACC,
	PARAM_ATT_KAL_IYAW,

	PARAM_GYRO_OFFSET_X,
	PARAM_GYRO_OFFSET_Y,
	PARAM_GYRO_OFFSET_Z,
	PARAM_CAL_TEMP,
	PARAM_CAL_GYRO_TEMP_FIT_X,
	PARAM_CAL_GYRO_TEMP_FIT_Y,
	PARAM_CAL_GYRO_TEMP_FIT_Z,
	PARAM_CAL_GYRO_TEMP_FIT_ACTIVE,

	PARAM_CAL_MAG_OFFSET_X,
	PARAM_CAL_MAG_OFFSET_Y,
	PARAM_CAL_MAG_OFFSET_Z,

	PARAM_CAL_PRES_DIFF_OFFSET,

	PARAM_ACC_OFFSET_X,
	PARAM_ACC_OFFSET_Y,
	PARAM_ACC_OFFSET_Z,

	PARAM_ACC_NAVI_OFFSET_X,
	PARAM_ACC_NAVI_OFFSET_Y,
	PARAM_ACC_NAVI_OFFSET_Z,

	PARAM_VISION_YAWCORRECT,
	PARAM_VISION_ANG_OUTLAYER_TRESHOLD,

	PARAM_VICON_MODE,
	PARAM_VICON_TAKEOVER_DISTANCE,
	PARAM_VICON_TAKEOVER_TIMEOUT,

	PARAM_SEND_DEBUGCHAN,

	PARAM_SEND_SLOT_ATTITUDE,
	PARAM_SEND_SLOT_RAW_IMU,
	PARAM_SEND_SLOT_REMOTE_CONTROL,
	PARAM_SEND_SLOT_CONTROLLER_OUTPUT,

	PARAM_SEND_SLOT_DEBUG_1,
	PARAM_SEND_SLOT_DEBUG_2,
	PARAM_SEND_SLOT_DEBUG_3,
	PARAM_SEND_SLOT_DEBUG_4,
	PARAM_SEND_SLOT_DEBUG_5,
	PARAM_SEND_SLOT_DEBUG_6,

	PARAM_PPM_SAFETY_SWITCH_CHANNEL,
	PARAM_PPM_TUNE1_CHANNEL,
	PARAM_PPM_TUNE2_CHANNEL,
	PARAM_PPM_TUNE3_CHANNEL,
	PARAM_PPM_TUNE4_CHANNEL,
	PARAM_PPM_THROTTLE_CHANNEL,
	PARAM_PPM_YAW_CHANNEL,
	PARAM_PPM_ROLL_CHANNEL,
	PARAM_PPM_NICK_CHANNEL,

	PARAM_GPS_MODE,
	PARAM_CAM_INTERVAL,
	PARAM_CAM_EXP,
	PARAM_CAM_ANGLE_X_OFFSET,
	PARAM_CAM_ANGLE_X_FACTOR,
	PARAM_CAM_ANGLE_Y_OFFSET,
	PARAM_CAM_ANGLE_Y_FACTOR,
	ONBOARD_PARAM_COUNT
	///< Store parameters in EEPROM and exglobal_param_idpose them over MAVLink parameter interface
} global_param_id;
*/


enum {
	PARAM_SYSTEM_ID = 0,
	PARAM_COMPONENT_ID,
	PARAM_SYSTEM_TYPE,
	PARAM_AUTOPILOT_TYPE,
	PARAM_SW_VERSION,
	PARAM_IMU_RESET,
	PARAM_ESC_CALIBRATION,

	PARAM_PID_ALTI_KP,
	PARAM_PID_ALTI_KI,
	PARAM_PID_ALTI_KD,
	PARAM_PID_ALTI_INTMAX,

	PARAM_PID_ROLL_KP,
	PARAM_PID_ROLL_KI,
	PARAM_PID_ROLL_KD,
	PARAM_PID_ROLL_INTMAX,

	PARAM_PID_PITCH_KP,
	PARAM_PID_PITCH_KI,
	PARAM_PID_PITCH_KD,
	PARAM_PID_PITCH_INTMAX,

	PARAM_PID_YAW_KP,
	PARAM_PID_YAW_KI,
	PARAM_PID_YAW_KD,
	PARAM_PID_YAW_INTMAX,

	PARAM_SERVO_0_ID,
	PARAM_SERVO_0_MIN,
	PARAM_SERVO_0_ZERO,
	PARAM_SERVO_0_MAX,

	PARAM_SERVO_1_ID,
	PARAM_SERVO_1_MIN,
	PARAM_SERVO_1_ZERO,
	PARAM_SERVO_1_MAX,

	PARAM_SERVO_2_ID,
	PARAM_SERVO_2_MIN,
	PARAM_SERVO_2_ZERO,
	PARAM_SERVO_2_MAX,

	PARAM_SERVO_3_ID,
	PARAM_SERVO_3_MIN,
	PARAM_SERVO_3_ZERO,
	PARAM_SERVO_3_MAX,

	PARAM_SERVO_4_ID,
	PARAM_SERVO_4_MIN,
	PARAM_SERVO_4_ZERO,
	PARAM_SERVO_4_MAX,

	PARAM_SERVO_5_ID,
	PARAM_SERVO_5_MIN,
	PARAM_SERVO_5_ZERO,
	PARAM_SERVO_5_MAX,

	PARAM_SERVO_6_ID,
	PARAM_SERVO_6_MIN,
	PARAM_SERVO_6_ZERO,
	PARAM_SERVO_6_MAX,

	PARAM_SERVO_7_ID,
	PARAM_SERVO_7_MIN,
	PARAM_SERVO_7_ZERO,
	PARAM_SERVO_7_MAX,

	ONBOARD_PARAM_COUNT
	///< Store parameters in EEPROM and expose them over MAVLink parameter interface
} global_param_id;

/*
// Testing new approach to save 1500 Bytes of RAM if we have 100 parameters.
// can we let the global_data.param_name pointer point to this array in flash?
static const char param_name[ONBOARD_PARAM_COUNT][ONBOARD_PARAM_NAME_LENGTH] = {
		[PARAM_SYSTEM_ID] = "SYS_ID",
		[PARAM_COMPONENT_ID] = "SYS_COMP_ID",
		[PARAM_SYSTEM_TYPE] = "SYS_TYPE",
		[PARAM_AUTOPILOT_TYPE] = "SYS_AP_TYPE",
		[PARAM_SW_VERSION] = "SYS_SW_VER",
		[PARAM_IMU_RESET] = "SYS_IMU_RESET",

		[PARAM_PID_ALTI_KP] = "SYS_ALTI_KP",
		[PARAM_PID_ALTI_KI] = "SYS_ALTI_KI",
		[PARAM_PID_ALTI_KD] = "SYS_ALTI_KD",
		[PARAM_PID_ALTI_INTMAX] = "SYS_ALTI_IMAX",

		[PARAM_PID_ROLL_KP] = "SYS_ROLL_KP",
		[PARAM_PID_ROLL_KI] = "SYS_ROLL_KI",
		[PARAM_PID_ROLL_KD] = "SYS_ROLL_KD",
		[PARAM_PID_ROLL_INTMAX] = "SYS_ROLL_IMAX",

		[PARAM_PID_PITCH_KP] = "SYS_PITCH_KP",
		[PARAM_PID_PITCH_KI] = "SYS_PITCH_KI",
		[PARAM_PID_PITCH_KD] = "SYS_PITCH_KD",
		[PARAM_PID_PITCH_INTMAX] = "PID_PITCH_IMAX",

		[PARAM_PID_YAW_KP] = "IMU_YAW_KP",
		[PARAM_PID_YAW_KI] = "IMU_YAW_KI",
		[PARAM_PID_YAW_KD] = "IMU_YAW_KD",
		[PARAM_PID_YAW_INTMAX] = "IMU_YAW_IMAX",
};
*/
void param_defaults(void);

void param_read_all(void);
void param_write_all(void);

void handle_param(int i);

float get_param_value(uint16_t param_id);
int set_param_value(uint16_t param_id, float value);
int set_param_value_with_name(uint8_t *param_id, float value);

int get_param_id_with_name(uint8_t *param_id);

char *get_param_name(char *name, uint16_t param_id);

#endif /* PARAM_H_ */
